﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class SalesLeads : BasePage
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                //this.txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                // 按钮权限控制
                //ControlAuthority(new List<LinkButton> { btnAdd, btnUpdate, btnDelete }, 3);
                //LoadData();
            }
        }

        [WebMethod]
        public static string GetDatas(string ledCode, string ledName, string ledDate, string orderField, string orderValue, int pageIndex, int pagePerSize)
        {
            string strResult;
            string strWhere = "";
            if (!string.IsNullOrEmpty(ledCode))
                strWhere += string.Format(" and FNumber like '%{0}%'", ledCode);
            if (!string.IsNullOrEmpty(ledName))
                strWhere += string.Format(" and FComName like '%{0}%'", ledName);
            if (!string.IsNullOrEmpty(ledDate))
            {
                strWhere += string.Format(" and convert(char(10),FDate,120)='{0}'", ledDate);
            }
            ICustBussinessoppBLL bussBLL = (ICustBussinessoppBLL)IocContainer.Instance.Resolve<ICustBussinessoppBLL>();
            //DataTable dt = bussBLL.Query(strWhere);
            StringBuilder strOrderBy = new StringBuilder();
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  FDate  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  desc ");
            int totalRecords = 0;
            int totalPages = 0;
            DataTable dt = bussBLL.Query(strWhere,strOrderBy.ToString(),pagePerSize,pageIndex, out totalRecords);
            totalPages = (totalRecords + pagePerSize - 1) / pagePerSize;
            strResult = SerializationHelper.DataTable2Json(dt);
            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";
        }

        [WebMethod]
        public static Boolean Delete(int id)
        {

            Boolean result = false;
            try
            {
                ICustBussinessoppBLL bussBLL = (ICustBussinessoppBLL)IocContainer.Instance.Resolve<ICustBussinessoppBLL>();
                bussBLL.Delete(id);
                result = true;
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
            return result;
        }

        //public void LoadData()
        //{
        //    try
        //    {
        //        StringBuilder strs = new StringBuilder();
        //        string strWhere = "";
        //        if (!string.IsNullOrEmpty(this.txtCode.Text.Trim()))
        //            strWhere += string.Format(" and FNumber like '%{0}%'", this.txtCode.Text.Trim());
        //        if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
        //            strWhere += string.Format(" and FComName like '%{0}%'", this.txtName.Text.Trim());
        //        if (!string.IsNullOrEmpty(this.txtDate.Text.Trim()))
        //        {
        //            strWhere += string.Format(" and convert(char(10),FDate,120)='{0}'", this.txtDate.Text.Trim());
        //        }
        //        DataTable dt = bussBLL.Query(strWhere);
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            foreach (DataRow dataRow in dt.Rows)
        //            {
        //                strs.AppendFormat("<tr rel='{0}'>", dataRow["FItemId"]);
        //                strs.AppendFormat("<td><a href='javascript:void(0)' rel='SalesLeadsDetail.aspx?LeadId={0}'>{1}</a></td>", dataRow["FItemId"], dataRow["FNumber"]);
        //                strs.AppendFormat("<td>{0}</td><td>{1}</td>", dataRow["FComName"], dataRow["FAddress"]);
        //                strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FProducts"], dataRow["FWebsite"], dataRow["EmpName"]);
        //                strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FArea"], dataRow["FChannel"], dataRow["FDate"]);
        //                strs.AppendFormat("<td>{0}</td>", dataRow["FMakerName"]);
        //                strs.AppendFormat("<td><a href='javascript:void(0)' class='openWindow' id='{0}'><span>设置活动</span></a>&nbsp;&nbsp;", dataRow["FItemId"]);
        //                strs.AppendFormat("<a href='javascript:void(0)' class='openWindow2' id='{0}'><span>设置商业报价</span></a></td>", dataRow["FItemId"]);                   
        //                strs.Append("</tr>");
        //            }
        //        }
        //        litLeadsList.Text = strs.ToString();
        //    }
        //    catch (Exception exception)
        //    {
        //        Logger.Instance.Error(exception);
        //        Alert("error:" + exception.Message);
        //    }
        //}

        //protected void btnSearch_Click(object sender, EventArgs e)
        //{
        //    LoadData();
        //}

        //protected void btnhidDelete_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        int Id = 0;
        //        int.TryParse(this.selectedLeadsID.Value.Trim(), out Id);
        //        bussBLL.Delete(Id);
        //        LoadData();
        //    }
        //    catch (Exception exception)
        //    {
        //        Logger.Instance.Error(exception);
        //    }
        //}
    }
}